package com.zib.bilibili.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.zib.bilibili.R;
import com.zib.bilibili.adapter.MyPagerAdapter;
import com.zib.bilibili.utils.KeyBoardUtils;
import com.zib.bilibili.view.fragment.LiveFragment;
import com.zib.bilibili.view.fragment.SubareaFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    private Context mContext = this;
    private TabLayout tabLayoutHome;
    private ViewPager viewPagerHome;
    private MyPagerAdapter adapter = null;
    private List<Fragment> totalList = new ArrayList<>();
    private String[] arrTabTitles = null;

    //popupWindow
    private PopupWindow popupWindow;
    private ImageView imageView_back;
    private ImageView imageView_scan;
    private ImageView imageView_query;
    private EditText editText_search;
    private View view_main;
    private String userName = "";
    private String userIcon = "";

    private ImageView imageView_login;
    private TextView textView_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initData();
        //初始化控件
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initData() {
        arrTabTitles = getResources().getStringArray(R.array.arrTabTitles);
        for (int i = 0; i < arrTabTitles.length; i++) {
            if (i == 3) {
                totalList.add(SubareaFragment.newInstance());
            } else {
                totalList.add(LiveFragment.newInstance());
            }
        }

    }

    private void initView() {


        View view = getLayoutInflater().inflate(R.layout.search_popwindow_main, null);
        imageView_back = (ImageView) view.findViewById(R.id.imageView_back_searchPopwindow);
        imageView_scan = (ImageView) view.findViewById(R.id.imageView_scan_searchPopwindow);
        imageView_query = (ImageView) view.findViewById(R.id.imageView_query_searchPopwindow);
        editText_search = (EditText) view.findViewById(R.id.editView_searchPopwindow);


        view_main = findViewById(R.id.view_main);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 取得焦点
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        /**设置PopupWindow弹出和退出时候的动画效果*/
        popupWindow.setAnimationStyle(R.style.searchAnimation);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.getChildAt(0).setVerticalScrollBarEnabled(false);
        navigationView.setNavigationItemSelectedListener(this);


        tabLayoutHome = (TabLayout) findViewById(R.id.tabLayout_home);
        viewPagerHome = (ViewPager) findViewById(R.id.viewPager_home);
        adapter = new MyPagerAdapter(getSupportFragmentManager(), totalList, arrTabTitles);
        viewPagerHome.setAdapter(adapter);
        tabLayoutHome.setupWithViewPager(viewPagerHome);

        View headerView = navigationView.getHeaderView(0);
        imageView_login = (ImageView) headerView.findViewById(R.id.imageView);
        textView_login = (TextView) headerView.findViewById(R.id.textView);

        imageView_back.setClickable(true);
        imageView_login.setClickable(true);
        imageView_back.setOnClickListener(this);
        imageView_scan.setOnClickListener(this);
        imageView_query.setOnClickListener(this);
        imageView_login.setOnClickListener(this);
        textView_login.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_search) {
            popupWindow.showAtLocation(view_main, Gravity.TOP, 0, 0);
            //打开软键盘
            KeyBoardUtils.openKeybord(editText_search, mContext);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigateUp() {
        return super.onNavigateUp();
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            // Handle the camera action
        } else if (id == R.id.nav_download) {

        } else if (id == R.id.nav_collect) {

        } else if (id == R.id.nav_history) {
            Toast.makeText(HomeActivity.this, "蒙多说想去哪就去哪", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_people) {

        } else if (id == R.id.nav_account) {

        } else if (id == R.id.nav_color) {

        } else if (id == R.id.nav_shop) {

        } else if (id == R.id.nav_setting) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.imageView_back_searchPopwindow:
                if (popupWindow.isShowing()) {
                    //关闭
                    popupWindow.dismiss();
                    //关闭软件盘
                    //KeyBoardUtils.closeKeybord(editText_search, context);
                }
                break;
            case R.id.imageView_scan_searchPopwindow:
                //打开扫描界面扫描条形码或二维码
                Intent openCameraIntent = new Intent(mContext, CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
                break;
            case R.id.imageView_query_searchPopwindow:
                Toast.makeText(mContext, "长得不帅,不给搜...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.imageView | R.id.textView:
                Toast.makeText(HomeActivity.this, "干嘛,,,你要登录/?", Toast.LENGTH_SHORT).show();
                Intent intent_lagin = new Intent();
                intent_lagin.setClass(mContext, LoginActivity.class);
                startActivity(intent_lagin);
                break;

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Toast.makeText(HomeActivity.this, "你要的扫描结果:" + scanResult, Toast.LENGTH_SHORT).show();
        }
        if (requestCode == 1) {
            Bundle bundle = data.getExtras();
            userName = bundle.getString("userName");
            userIcon = bundle.getString("userIcon");

        }
    }
}
